<?php
/**
 * @version : 1.0
 * @since June-2008
 * @package : lib
 * @subpackage struct
 * @name :  CDateTime
 * @todo:  Lớp  cơ sở CDateTime. Thao tác với dữ liệu Date/Time
*/
class CDateTime
{
	public $time;
	static $date_format = 'd/m/Y';
	static $data_date_format = 'Y-m-d';
	static $date_time_format = 'd/m/Y H:i:s';
	static $time_format = 'H:i:s';
	static $remain_time_format = '%d ngày %hgiờ %iphút %sgiây';
	function __construct($time)
	{
		if(preg_match('/^\d{4,4}\-\d{1,2}\-\d{1,2}$/', $time, $patterns))
		{
			$this->time = strtotime($time);
		}
		elseif(preg_match('/^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/', $time, $patterns) or preg_match('/^(\d{1,2})\/(\d{1,2})\/(\d{2,4})\s(\d{1,2})\:(\d{1,2})\:(\d{1,2})$/', $time, $patterns))
		{
			if(strlen($patterns[3]) == 2)
			{
				if(intval($patterns[3])<50)
				{
					  $patterns[3] = '20'.$patterns[3];
				}
				else
				{
					  $patterns[3] = '19'.$patterns[3];
				}
			}
			if(CDateTime::$date_format[0] == 'd')
			{
				$this->time = strtotime($patterns[2].'/'.$patterns[1].'/'.$patterns[3]);
			}
			else
			{
				$this->time = strtotime($patterns[1].'/'.$patterns[2].'/'.$patterns[3]);
			}
			if(isset($patterns[4]))
			{
				$this->time += $patterns[4]*3600+$patterns[5]*60+$patterns[6];
			}
		}
		elseif(preg_match('/[^\d\.\s]/',$time))
		{
			$this->time = strtotime($time);
		}
		else
		{
			$this->time = $time;
		}
	}
	function date($format = false)
	{
		return date($format?$format:CDateTime::$date_format,$this->time);
	}
	function sql_date($format = false)
	{
		return date($format?$format:CDateTime::$data_date_format,$this->time);
	}
	function date_time($format = false)
	{
		return date($format?$format:CDateTime::$date_time_format,$this->time);
	}
	function time($format = false)
	{
		return date($format?$format:CDateTime::$time_format,$this->time);
	}
	function micro_time($format = false)
	{
		return date($format?$format:CDateTime::$date_time_format,floor($this->time)).' '.($this->time - floor($this->time));
	}
	function duration($date_time)
	{
		return $date_time->time - $this->time;
	}
	function elapse_time($format = false, $time = null)
	{
		
		if($time == null)
		{
			$time = time();
		}
		if(!$format)
		{
			  $format = CDateTime::$remain_time_format;
		}
		
		$remain = $time - $this->time;
		if($remain < 24*3600)
		{
			  $pos = strpos($format,'%d');
			  $format = str_replace(substr($format,$pos,strpos($format,' ',$pos)-$pos),'',$format);
		}
		if(!($remain%60))
		{
			  $pos = strpos($format,'%s');
			  $format = str_replace(substr($format,$pos),'',$format);
		}
		if(!($remain%3600))
		{
			  $pos = strpos($format,'%i');
			  $format = str_replace(substr($format,$pos),'',$format);
		}
		if(!($remain%(24*3600)))
		{
			  $pos = strpos($format,'%h');
			  $format = str_replace(substr($format,$pos),'',$format);
		}
		
		return str_replace(array('%d','%h','%i','%s'),array(floor($remain/(24*3600)),floor($remain/(3600))%24,floor($remain/60)%60,$remain%60),$format);
	}
	
	static 	function get_promotion_time(){
		$time_points = array(10080, 14400, 28800, 43200, 57600,72000);
		$this_day = getdate();
		$param = round(5000*sin((2*$this_day['yday']/365)*pi())) + ($this_day['year']-1984)*7 + 50*rand(0,100);
		foreach ($time_points as &$val){
			$val += $param;
		}
		return $time_points;
	}
}
function CDateTime($time)
{
      return new CDateTime($time);
}

?>